package com.atguigu.blog.web;

import com.atguigu.blog.pojo.About;
import com.atguigu.blog.service.AboutService;
import com.atguigu.blog.service.LogInfoService;
import com.atguigu.blog.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 关于我们
 * @author bruce lee
 */
@Controller
@RequestMapping("")
public class AboutController {

    @Autowired
    private AboutService aboutService;
    @Autowired
    private LogInfoService logInfoService;
    @Autowired
    private IdWorker idWorker;

    @GetMapping("/about")
    public String getAbout(Map<String, Object> map, HttpServletRequest request) {
        QueryWrapper<About> queryWrapper = new QueryWrapper<>();
        About one = aboutService.getOne(queryWrapper);
        map.put("about", one);
        logInfoService.saveLogInfo(request, "关于");
        return "about";
    }

    @GetMapping("/aboutEdit")
    public String aboutEdit(Map<String, Object> map) {
        QueryWrapper<About> queryWrapper = new QueryWrapper<>();
        About one = aboutService.getOne(queryWrapper);
        if (one!=null) {
            // 修改
            map.put("about", one);
        }
        return "aboutEdit";
    }

    @PostMapping("/aboutSaveOrUpdate")
    public String aboutSaveOrUpdate(About about) {
        QueryWrapper<About> queryWrapper = new QueryWrapper<>();
        About one = aboutService.getOne(queryWrapper);
        if (one!=null) {
            String id = one.getId();
            about.setId(id);
            about.setUpdateDate(new Date());
            aboutService.updateById(about);
        } else {
            about.setId(idWorker.nextId()+"");
            aboutService.save(about);
        }
        return "redirect:/ucenter";
    }
}
